<?php
		if (session_id() == '') {
	    session_start();
	}
		if (isset($_POST['arrayid'])and isset($_POST['kogus'])){
			if($_POST['kogus']==0){
				$korv=$_SESSION['korv'];
				unset($korv[$_POST['arrayid']]);
				$_SESSION['korv']=$korv;
			}
			else {
				$korv=$_SESSION['korv'];
				$korv[$_POST['arrayid']]->kogus=$_POST['kogus'];
			}
		}
		
		if(isset($_SESSION["kasutaja"])and isset($_POST['telli'])){
			if (session_id() == '') {
		    session_start();
			}
		require("konf.php");
		$yhendus=new mysqli($masin, $kasutaja, $parool, $baas);
		
		$kask=$yhendus->prepare("SELECT distinct addres.addres_id, addres.addres, asula.nimetus, indeks, riik.nimetus FROM addres, isik, riik, asula 
		WHERE asula.asula_id=addres.asula_id and addres.riik_id=riik.riik_id and (addres.isik_id=?)");
		//// ajax for dynamic country and city
		$yhendus->query("SET NAMES utf8");
		$kask->bind_param("i", $_SESSION["id"]);
		$kask->bind_result($id, $address, $asula, $indeks, $riik);
		$kask->execute();
		$count=0;
		$addressid=array();
		while($kask->fetch()){
			$adr=new stdClass();
			$adr->id=$id;
			$adr->address=$address;
			$adr->asula=$asula;
			$adr->indeks=$indeks;
			$adr->riik=$riik;
			array_push($addressid, $adr);
			$count++;
		}
		
		
		
		echo $yhendus->error;
		$kask->close();
		$yhendus->close();
		
		}
		
		if(isset($_POST['makseviis'])and isset($_POST['aadr'])){
			require("konf.php");
			$yhendus=new mysqli($masin, $kasutaja, $parool, $baas);
			
			$q="INSERT into tellimus (isik_id, addres_id, kommentaar) VALUES (?,?,?)";
			$kask=$yhendus->prepare($q);
			$kask->bind_param("iis", $_SESSION['id'], $_POST['aadr'], $_POST['comments']);
			$tid=$kask->insert_id;
			
			
			$kask->execute();
			$tid=$kask->insert_id;
			$kask->close();
			foreach ($_SESSION['korv'] as $toode) {
				$q2="INSERT into t_tooted (tellimus_id, menu_kirje_id, kogus, hind) VALUES(?,?,?,?)";
				$kask=$yhendus->prepare($q2);
				$kask->bind_param("iiii", $tid, $toode->id, $toode->kogus, $toode->hind);
				$kask->execute();
				$kask->close();
			}
			$yhendus->close();
			unset($_SESSION['korv']);
			
		}
?>

<!DOCTYPE html>
<html>
<head>
<div id="wrapper">
<?php
require("pais.php")
?>
</head>

<body>
<div id="leftcolumn">
<h1>Tellimuste esitamine</h1>
<?php
	if(isset($_SESSION['korv'])){
	$korv=$_SESSION['korv'];
	$summa=0;
	echo"<div id ='left_left'><table>";
	$count=0;
	foreach($korv as $kaup){
		
	
				echo"
				<tr>
				<td><IMG HEIGHT='40' WIthH='20' SRC='$kaup->pilt' ></td>
				<td>$kaup->nimi</td>
				<td>
				Kogus: $kaup->kogus x $kaup->hind €</td> <td>Kokku: ".$kaup->kogus*$kaup->hind."€</td></td>";
				$summa+=$kaup->kogus*$kaup->hind; 
				echo"
				<td width=120></td><td><form action=? method='post'>
				<input type='number' name='kogus' value='$kaup->kogus'>
				<input type='hidden' name='arrayid' value='$count'>
				<input type='submit' name='uuenda' value='Uuenda'>
				</form></td></div>";	
				$count++;			
		}
	echo"</table></div>
	";
	echo"Kokku : $summa €<hr>";
	if(isset($tid)){
		echo "$tid";
	}
	}
	else {
		echo"OSTUKORV ON TÜHI";
	}
	
	if(isset($_SESSION['korv']))
	{
		if(isset($_SESSION['kasutaja'])and !isset($_POST['telli']))
		{
			echo"<h2>Alusta tellimist</h2>";
			echo"<form action=? method='post'><input type='submit' name='telli' value='tellima'></form>";
		}
		else if(isset($_POST['telli'])){
			echo"<form action=? method='post'>
			<div align='center'>
			<h3>Makseviis</h3>
			<input type='radio' name='makseviis' value='Sularaha' checked>Sularaha<br><br>
			<h3>Vali address kuhu soovid tellida</h3>";
			foreach ($addressid as $adr) {
			
			echo"<table>
			<tr><td>$adr->address</td></tr>
			<tr><td>$adr->asula &nbsp; $adr->indeks</td></tr>
			<tr><td>$adr->riik</td></tr>
			<tr><td><input type='radio' name='aadr' value='$adr->id'>
			</table>
			
			<hr>";
			}	
			echo"
			Kommentaar:
			<textarea name='comments' cols='25' rows='5'>
			</textarea><br>
			<input type='submit' value='Kinnita tellimus'></div>
			</form>";
		}
		else
		{
			echo"<div id='vasak'><h2>Tellimiseks on vaja sisse logida</h2>";
			echo"<a href='./login.php'>Logi Sisse</a></div>";
			echo"<div id='parem'><h2>Pole veel kasuajat?</h2>
			<a href='./register.php'>Registreeruma</a></div>";
		}
	}
?>

</div>
</div>
<div id="rightcolumn">
<?php require("korv.php");?>
</div>
</div>
</body>
</html>